package com.yantong.controller.api;

import com.yantong.annotation.Auth;
import com.yantong.controller.BaseController;
import com.yantong.pojo.CommisLogsParams;
import com.yantong.pojo.CommissionLog;
import com.yantong.result.ResultVo;
import com.yantong.service.CommissionService;
import com.yantong.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @author: juzi
 * @date: 2021/10/25
 * @time: 16:12
 */
@ApiIgnore
@Controller
@RequestMapping("/wxapi/commission")
public class ApiCommissionController extends BaseController {

    @Autowired
    private CommissionService commissionService;


    /**
     * 佣金统计
     */
    @RequestMapping("/total")
    @ResponseBody
    @Auth(arg = 0)
    public ResultVo getTotalCommission(Long memberId, Integer isGrant) {
        double total = commissionService.commissionCount(memberId, isGrant);
        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        resultVo.put("data", total);
        return resultVo;
    }

    /**
     * 佣金明细
     */
    @RequestMapping("/log")
    @ResponseBody
    @Auth
    public ResultVo getCommissionLog(CommisLogsParams params, int offset, int limit) {
        PageUtils<CommissionLog> logs = commissionService.listCommissionLogs(params, offset, limit);
        params.setIsGrant(0);
        BigDecimal allCommisionNoGrant = commissionService.countCommissionLogs(params);
        params.setIsGrant(1);
        BigDecimal allCommisionIsGrant = commissionService.countCommissionLogs(params);
        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        resultVo.put("data", logs);
        resultVo.put("allCommisionNoGrant", allCommisionNoGrant);
        resultVo.put("allCommisionIsGrant", allCommisionIsGrant);
        return resultVo;
    }

    /**
     * 提成统计
     */
    @RequestMapping("/sum")
    @ResponseBody
    @Auth(arg = 0)
    public ResultVo getCommissionSum(Long shopId, Integer type, int offset, int limit) {
        Map<String, Object> info = commissionService.getMidGrantCommissionInfo(shopId, type, offset, limit);
        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        resultVo.put("data", info);
        return resultVo;
    }
}
